%% Replication file of Baqaee, Burstein, and Koike-Mori (2023) %%%%%%%%%%%%%%
% Prepared by Yasutaka Koike-Mori (UCLA) 
clear all
close all
addpath('functions')
addpath('subroutines')
addpath('data')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Main Results: UK Application
if ~isfile('data/DataforMatlab/UK_Budget.mat')
    run("subroutines/MatlabDataConstruction.m")
end 
load 'data/DataforMatlab/UK_Budget.mat'
load 'data/DataforMatlab/pvec_tmp.mat'
I = size(B_vec,1);N = size(B_vec,2);T = size(B_vec,3);pvec = reshape(pvec_tmp',[I,1,T]);
% Money Metrics for UK

[U_vec] = CalMoneyMetric(I_vec, B_vec, pvec, 0);
run("subroutines/sub_Figure_3.m")
run("subroutines/sub_Figure_5.m") 

%run("subroutines/sub_for_missing_price.m")
% Figure 4 require gramm package
% https://www.mathworks.com/matlabcentral/fileexchange/54465-gramm-complete-data-visualization-toolbox-ggplot2-r-like
%run("subroutines/sub_Figure_4.m")

%% Missing Price Application
clear all
if ~isfile('data/DataforMatlab/UK_Budget.mat')
    run("subroutines/MatlabDataConstruction.m")
end 
load 'data/DataforMatlab/UK_Budget.mat'
load 'data/DataforMatlab/pvec_tmp.mat'
subset = [1:9 11:16];
Bx_vec = sum(B_vec(subset,:,:),1);
Bxi_vec = B_vec(subset,:,:)./Bx_vec;
[I, N, T] = size(Bxi_vec);
pvec = reshape(pvec_tmp(:,subset)',[I,1,T]);
run("subroutines/sub_engel_OLS.m")
[U_vec,WP_total_vec, WP_FS_vec] = CalMoneyMetric_Unobserved(I_vec, Bx_vec, Bxi_vec, pvec, sigmaM, 0);

run("subroutines/sub_Figure_6.m")
run("subroutines/sub_Figure_7.m")
run("subroutines/sub_Figure_8.m")
run("subroutines/sub_Figure_9_10.m")

%% Illustrations
clear all
run("subroutines/sub_Figure_2.m")
run("subroutines/sub_GenerateArtificialData.m")
load data/DataforMatlab/NhCES_const_artificial.mat
flag = 'nh';
run("subroutines/sub_Figure_1.m")
flag = 'h';
load data/DataforMatlab/NhCES_const_homothetic_artificial
run("subroutines/sub_Figure_1.m")

